//
//  YYEWrongRecordViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/5.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEWrongRecordViewController.h"
#import "YYETopicRecordTableViewCell.h"
#import "YYEExampleViewController.h"

@interface YYEWrongRecordViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) YYEBaseTableView *tableView;
@property (nonatomic,strong) NSArray *datas;

@end

@implementation YYEWrongRecordViewController

static NSString * recordCell = @"recordCell";

- (void)dealloc
{
    NSLog(@"");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self configUI];
    
    if (self.mark == 1) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        headerView.backgroundColor = GeneralBackColor;
        self.tableView.tableHeaderView = headerView;
    }


}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getList];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getList) name:@"kExamNotification" object:nil];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)getList {
    
    NSDictionary *dic = @{
//                          @"cid":self.cid,
//                          @"p":@(self.page),
//                          @"size":@10,
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token
                          };
    NSMutableDictionary *dicM = dic.mutableCopy;
    [dicM setValue:self.cid forKey:@"cid"];
    
    [SVProgressHUD show];
    [AFNHelper post:GetList parameters:dic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);

        if ([responseObject[@"status"] isEqual:@1]) {
            
            NSArray *commandArr = [YYEWrongRecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            self.datas = commandArr;
            [self.tableView reloadData];
        }
//        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
}

- (void)configUI {
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[YYETopicRecordTableViewCell class] forCellReuseIdentifier:recordCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

#pragma mark -- UITableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.datas.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYETopicRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:recordCell];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.datas[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 82;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YYEWrongRecordModel *model = self.datas[indexPath.row];

    if (model.process.integerValue != 0) {
        
        // "process": 1, //进度(0未达到进度 1开始答题 2继续答题 3查看答题
        // "type": 2, // 1是习题 2是考试
        YYEExampleViewController *vc = [[YYEExampleViewController alloc]init];
        vc.hid = model.hid;
        vc.mid = model.mid;
        vc.type = model.type;
//        vc.process = model.process;
        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
